package com.chen.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.common.Result;
import com.chen.mapper.BookMapper;
import com.chen.pojo.Book;
import com.chen.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Resource
    private BookMapper bookMapper;

    /**
     * 添加图书
     *
     * @param book
     * @return
     */
    public Result<?> addBook(Book book) {
        return Result.success(bookMapper.insert(book));
    }

    /**
     * 更新图书
     *
     * @param book
     * @return
     */
    public Result<?> updateBook(Book book) {
        return Result.success(bookMapper.updateById(book));
    }


    /**
     * 删除图书
     */
    public Result<?> deleteBook(Long id) {
        return Result.success(bookMapper.deleteById(id));
    }

    /**
     * 根据id查询图书
     */

    public Result<?> selectById(Long id) {
        return Result.success(bookMapper.selectById(id));
    }

    /**
     * 查询所有图书
     */

    public Result<?> selectAll() {
        return Result.success(bookMapper.selectList(null));
    }


    /**
     * 分页
     * @param pageNum
     * @param pageSize
     * @param search
     * @return
     */
    public Result<?> findPage(Integer pageNum, Integer pageSize, String search) {
      //模糊查询
        LambdaQueryWrapper<Book> wrapper = Wrappers.<Book>lambdaQuery().like(Book::getName, search);
        if (StrUtil.isBlank(search)) {
            wrapper.like(Book::getAuthor, search);
        }
        Page<Book> bookPage = bookMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(bookPage);
    }

}
